package com.zjsru.oneDay202206;

/**
 * @Author: likew
 * @Date: 2022/7/24
 * 公交站间的距离
 *  环形公交路线上有n个站，按次序从0到n - 1进行编号。我们已知每一对相邻公交站之间的距离，distance[i]表示编号为i的车站和编号为(i + 1) % n的车站之间的距离。
 *
 * 输入：distance = [1,2,3,4], start = 0, destination = 1
 * 输出：1
 * 解释：公交站 0 和 1 之间的距离是 1 或 9，最小值是 1。
 *
 */
public class distanceBetweenBusStops {
    public int distanceBetweenBusStops(int[] distance, int start, int destination) {
        int len = distance.length,i = start, j = start;
        int ans1 = 0;
        int ans2 = 0;
        while(i != destination){
            ans1 += distance[i];
            //走到最后一位但是还没有到目的地
            if(++i == len){
                i = 0;
            }
        }
        while(j != destination){
            if(--j < 0) j = len - 1;
            ans2 += distance[j];
        }
        return Math.min(ans1,ans2);
    }
    
    public static void main(String[] args) {
        distanceBetweenBusStops distanceBetweenBusStops = new distanceBetweenBusStops();
        int[] distance = new int[]{1,2,3,4};
        int start = 0;
        int destination = 1;
        System.out.println(distanceBetweenBusStops.distanceBetweenBusStops(distance, start, destination));
    }
}
